library interfaz;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

{%TogetherDiagram 'ModelSupport_interfaz\default.txaPackage'}
{%TogetherDiagram 'ModelSupport_interfaz\interfaz\default.txaPackage'}
{%TogetherDiagram 'ModelSupport_interfaz\default.txvpck'}
{%TogetherDiagram 'ModelSupport_interfaz\interfaz\default.txvpck'}

uses
  Windows,
  SysUtils,
  AdPort,
  OoMisc,
  AdSelCom,
  ExtCtrls,
  IntList,
  Contnrs,
  Math,
  Dialogs,
  Classes,
  InterfazSerial;

{********* MODELO DE PROCS PARA EXPORTAR ************}
function Test:word; stdcall;
begin
  Result := 1;
end;
{****************************************************}

function NewInterfaz(Port:integer):word; stdcall;
var
  s: TInterfaz;
begin
  if IsPortAvailable(Port) then
  begin
    s := TInterfaz.Create(Port);
    Interfaces.Add(s);
    curInterfaz := Interfaces.Count - 1;
    maxInterfaz := maxInterfaz + 1;
    if (Interfaces.Items[curInterfaz] as TInterfaz).Com.Open then
      Result := curInterfaz
    else Result := High(Result);
  end else
    Result := High(Result);
end;

function GetBuffer: word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetBufferFirst;
end;

procedure CloseInterfaz; stdcall;
begin
    (Interfaces.Items[curInterfaz] as TInterfaz).Close;
end;

function GetBufferCount: word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).Buffer.IntCount;
end;


procedure TalkInterfaz(num: integer); stdcall;
begin
  curInterfaz := num;
end;

procedure TalkMotor(Motors: Byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).TalkMotor(Motors);
end;

procedure MotorOn; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorOn;
end;

procedure MotorOff; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorOff;
end;

procedure MotorInverse; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorInverse;
end;

procedure MotorThisWay; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorThisWay;
end;

procedure MotorThatWay; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorThatWay;
end;

procedure MotorCoast; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorCoast;
end;

procedure MotorPower(Power: byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).MotorPower(Power);
end;

procedure AllMotorsOff;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).AllMotorsOff;
end;

procedure TalkPAP(PAP: byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).TalkPAP(PAP);
end;

procedure PAPSpeed(Speed: byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).PAPSpeed(Speed);
end;

procedure PAPSteps(Steps: byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).PAPSteps(Steps);
end;

procedure ServoPos(Pos: Byte); stdcall;
begin
    (Interfaces.Items[curInterfaz] as TInterfaz).ServoPos(Pos);
end;

procedure TalkSensor(Sensor: Byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).TalkSensor(Sensor);
end;

function GetSensor:Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetSensor;
end;

procedure SensorBurst(Sensors: Byte; Slow: Byte); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).SensorBurst(Sensors,Slow);
end;

procedure StopBurst; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).StopBurst;
end;

function GetBurstValue:Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetBurstValue;
end;

function GetLastBurstValue: Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetLastBurstValue;
end;

function GetBurstTick:LongWord; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetBurstTick;
end;

function BurstCount:LongWord; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).BurstCount;
end;

function NextBurst: Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).NextBurst;
end;

procedure ClearBurst; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).ClearBurst;
end;

function GetSensorValue: LongWord; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetSensorValue;
end;

function GetSensorTick: LongWord; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).GetSensorTick;
end;

function NextSensorValue: Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).NextSensorValue;
end;

procedure ClearSensorValues; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).ClearSensorValues;
end;

function SensorValuesCount: LongWord; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).SensorValuesCount;
end;

procedure SetTimer(Interval: Word); stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).SetTimer(Interval);
end;

procedure ClearTimer; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).ClearTimer;
end;

procedure SaveFile; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).SaveFile;
end;

procedure CloseFile; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).CloseFile;
end;

procedure SaveBurstToFile; stdcall;
begin
  (Interfaces.Items[curInterfaz] as TInterfaz).SaveBurstToFile;
end;

function SendProgramByte(B: Byte; Reverse: Byte): Word; stdcall;
begin
  Result := (Interfaces.Items[curInterfaz] as TInterfaz).SendProgramByte(B,Reverse);
end;

procedure SetBaudRate(BR: integer); stdcall;
begin
  BaudRate :=  BR;
end;

exports Test, NewInterfaz, CloseInterfaz, GetBuffer, GetBufferCount,
        TalkInterfaz, MotorOn, MotorOff, MotorInverse, AllMotorsOff,
        TalkMotor, MotorThisWay, MotorThatWay, MotorCoast,
        MotorPower, TalkPAP, PAPSpeed, PAPSteps, ServoPos, TalkSensor,
        GetSensor, SensorBurst, StopBurst, GetBurstValue, GetBurstTick, BurstCount,
        GetLastBurstValue, NextBurst, ClearBurst, GetSensorValue, GetSensorTick, NextSensorValue,
        ClearSensorValues, SensorValuesCount, SetTimer, ClearTimer,
        SaveFile, CloseFile, SaveBurstToFile, SendProgramByte, SetBaudRate; // ..,etc,etc;

begin
  Interfaces := TObjectList.Create;
  maxInterfaz:= 0;
  curInterfaz:= 0;
  BaudRate := 9600;
end.
